package com.team3.ecommerce.controller;


import com.team3.ecommerce.domain.CommInOrder;
import com.team3.ecommerce.domain.Commodity;
import com.team3.ecommerce.domain.ShoppingCartItem;
import com.team3.ecommerce.domain.UserAuth;
import com.team3.ecommerce.service.CommodityService;
import com.team3.ecommerce.service.ShoppingCartService;
import com.team3.ecommerce.service.UserAuthService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ShoppingCartController {

    @Autowired
    private UserAuthService userAuthService;
    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private CommodityService commodityService;

    @GetMapping("/shoppingCart")
    public String shoppingCart(Model model, Principal principal) {
        String user_uid = userAuthService.findUserByPrincipal(principal).getUserUid();
        List<ShoppingCartItem> shoppingCartItemList = shoppingCartService.getUserShoppingCart(user_uid);
        List<Commodity> commodityList = new ArrayList<>();
        for (int i = 0; i < shoppingCartItemList.size(); i++) {
            commodityList.add(commodityService.findCommodityById(shoppingCartItemList.get(i).getCommUid()));
        }
        model.addAttribute("commodityList", commodityList);
        model.addAttribute("shoppingCartItemList", shoppingCartItemList);
        model.addAttribute("userUid", user_uid);
        return "shoppingCart.html";
    }

    @PostMapping("/shoppingCart")
    @ResponseBody
    public String shoppingCartUpdate(@ModelAttribute("userUid") String userUid,
                                     @ModelAttribute("commUid") String commUid,
                                     @ModelAttribute("commQuantity") int commQuantity,
                                     @ModelAttribute("itemChecked") String itemChecked) {
//        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
//        System.out.println("收到前端请求，现在是"+formatter.format(System.currentTimeMillis()));
        shoppingCartService.updateUserShoppingCart(userUid, commUid, commQuantity,itemChecked);
        return "ok";
    }

    @PostMapping("/shoppingCart/delete")
    public String shoppingCartDelete(String commUid, Principal principal) {
        shoppingCartService.deleteUserShoppingCartItem(userAuthService.findUserByPrincipal(principal).getUserUid(), commUid);
        return "redirect:/shoppingCart";
    }

    @PostMapping("/shoppingCart/add")
    public String shoppingCartAdd(String commUid, Principal principal) {
        shoppingCartService.addUserShoppingCartItem(userAuthService.findUserByPrincipal(principal).getUserUid(), commUid);
        return "redirect:/shoppingCart";
    }
}
